 

package com.zdkj.subway.stm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.dto.UserInfo;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwAuInternetTicketEquipment;
import com.zdkj.subway.stm.entity.SwStInternetTicketEquipmentDetails;
import com.zdkj.subway.stm.mapper.SwAuInternetTicketEquipmentMapper;
import com.zdkj.subway.stm.service.SwStInternetTicketEquipmentDetailsService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author songtuo
 * @date 2020-03-07 20:14:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstinternetticketequipmentdetails" )
@Api(value = "swstinternetticketequipmentdetails", tags = "管理")
public class SwStInternetTicketEquipmentDetailsController {

    private final  SwStInternetTicketEquipmentDetailsService swStInternetTicketEquipmentDetailsService;
    private final  SwAuInternetTicketEquipmentMapper swAuInternetTicketEquipmentMapper;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swStInternetTicketEquipmentDetails 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStInternetTicketEquipmentDetailsPage(Page page, SwStInternetTicketEquipmentDetails swStInternetTicketEquipmentDetails) {
        return R.ok(swStInternetTicketEquipmentDetailsService.page(page, Wrappers.query(swStInternetTicketEquipmentDetails)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStInternetTicketEquipmentDetailsService.getById(id));
    }

    /**
     * 新增
     * @param swStInternetTicketEquipmentDetails 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('generator_swstinternetticketequipmentdetails_add')" )
    public R save(@RequestBody SwStInternetTicketEquipmentDetails swStInternetTicketEquipmentDetails) {
        return R.ok(swStInternetTicketEquipmentDetailsService.save(swStInternetTicketEquipmentDetails));
    }

    /**
     * 修改
     * @param swStInternetTicketEquipmentDetails 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@pms.hasPermission('generator_swstinternetticketequipmentdetails_edit')" )
    public R updateById(@RequestBody SwStInternetTicketEquipmentDetails swStInternetTicketEquipmentDetails) {
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
        if("1".equals(swStInternetTicketEquipmentDetails.getReportModifyStatus())){
            SwAuInternetTicketEquipment swAuInternetTicketEquipment = swAuInternetTicketEquipmentMapper.selectOne(
                    Wrappers.<SwAuInternetTicketEquipment>lambdaQuery()
                            .eq(SwAuInternetTicketEquipment::getLineCode, swStInternetTicketEquipmentDetails.getLineCode())
                            .eq(SwAuInternetTicketEquipment::getStationCode, swStInternetTicketEquipmentDetails.getStationCode())
                            .eq(SwAuInternetTicketEquipment::getEquipmentCode, swStInternetTicketEquipmentDetails.getEquipmentCode())
                            .eq(SwAuInternetTicketEquipment::getRevenueDate, swStInternetTicketEquipmentDetails.getRevenueDate()));
            //存在就update发售数量，发售金额、差异金额、差异数量
            if(swAuInternetTicketEquipment!=null){
                swAuInternetTicketEquipment.setTotalNumberSales(swStInternetTicketEquipmentDetails.getSaleNumber());
                swAuInternetTicketEquipment.setSubtotalTotal(swStInternetTicketEquipmentDetails.getTotalSaleAmount());
				swAuInternetTicketEquipment.setRemark(swStInternetTicketEquipmentDetails.getRemark());

                swAuInternetTicketEquipment.setStartTime(dff.format(swStInternetTicketEquipmentDetails.getStartTime()));
                swAuInternetTicketEquipment.setEndTime(dff.format(swStInternetTicketEquipmentDetails.getEndTime()));
                long SettlementPeriod=(swStInternetTicketEquipmentDetails.getEndTime().getTime()-swStInternetTicketEquipmentDetails.getStartTime().getTime())/(24*60*60*1000);
                swAuInternetTicketEquipment.setSettlementPeriod(Integer.parseInt(String.valueOf(SettlementPeriod)));
                swAuInternetTicketEquipmentMapper.updateById(swAuInternetTicketEquipment);
            }else {
                SwAuInternetTicketEquipment swAuInternetTicketEquipment1 =new SwAuInternetTicketEquipment();
                swAuInternetTicketEquipment1.setLineCode(swStInternetTicketEquipmentDetails.getLineCode());
                swAuInternetTicketEquipment1.setStationCode(swStInternetTicketEquipmentDetails.getStationCode());
                swAuInternetTicketEquipment1.setStartTime(dff.format(swStInternetTicketEquipmentDetails.getStartTime()));
                swAuInternetTicketEquipment1.setEndTime(dff.format(swStInternetTicketEquipmentDetails.getEndTime()));
                swAuInternetTicketEquipment1.setTotalNumberSales(swStInternetTicketEquipmentDetails.getSaleNumber());
				swAuInternetTicketEquipment1.setRemark(swStInternetTicketEquipmentDetails.getRemark());

                swAuInternetTicketEquipment1.setSubtotalTotal(swStInternetTicketEquipmentDetails.getTotalSaleAmount());
                long SettlementPeriod=(swStInternetTicketEquipmentDetails.getEndTime().getTime()-swStInternetTicketEquipmentDetails.getStartTime().getTime())/(24*60*60*1000);
                swAuInternetTicketEquipment1.setSettlementPeriod(Integer.parseInt(String.valueOf(SettlementPeriod)));
                swAuInternetTicketEquipment1.setApprovalStatus(0);
                PigxUser user = SecurityUtils.getUser();
                swAuInternetTicketEquipment1.setCreatorId(user.getId());
                swAuInternetTicketEquipment1.setCreatorName(user.getNikeName());
                swAuInternetTicketEquipment1.setCreateTime(new Date());


                swAuInternetTicketEquipment1.setPassengerAttendantId(user.getId());
                swAuInternetTicketEquipment1.setPassengerAttendantJobNo(user.getUserCode());
                swAuInternetTicketEquipment1.setPassengerAttendantName(user.getNikeName());

                swAuInternetTicketEquipment1.setEquipmentCode(swStInternetTicketEquipmentDetails.getEquipmentCode());
                swAuInternetTicketEquipment1.setEquipmentName(swStInternetTicketEquipmentDetails.getEquipmentName());
                swAuInternetTicketEquipment1.setRevenueDate(swStInternetTicketEquipmentDetails.getRevenueDate());
                swAuInternetTicketEquipment1.setReportModifyStatus(swStInternetTicketEquipmentDetails.getReportModifyStatus());

                swAuInternetTicketEquipment1.setAttendancePersonId(user.getId());
                swAuInternetTicketEquipment1.setAttendancePersonJobNo(user.getUserCode());
                swAuInternetTicketEquipment1.setAttendancePersonName(user.getNikeName());

                swAuInternetTicketEquipmentMapper.insert(swAuInternetTicketEquipment1);

            }
        }

        return R.ok(swStInternetTicketEquipmentDetailsService.updateById(swStInternetTicketEquipmentDetails));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@pms.hasPermission('generator_swstinternetticketequipmentdetails_del')" )
    public R removeById(@PathVariable Integer id) {
        SwStInternetTicketEquipmentDetails swStInternetTicketEquipmentDetails = swStInternetTicketEquipmentDetailsService.getById(id);
        if("1".equals(swStInternetTicketEquipmentDetails.getReportModifyStatus())){
            SwAuInternetTicketEquipment swAuInternetTicketEquipment = swAuInternetTicketEquipmentMapper.selectOne(
                    Wrappers.<SwAuInternetTicketEquipment>lambdaQuery()
                            .eq(SwAuInternetTicketEquipment::getLineCode, swStInternetTicketEquipmentDetails.getLineCode())
                            .eq(SwAuInternetTicketEquipment::getStationCode, swStInternetTicketEquipmentDetails.getStationCode())
                            .eq(SwAuInternetTicketEquipment::getEquipmentCode, swStInternetTicketEquipmentDetails.getEquipmentCode())
                            .eq(SwAuInternetTicketEquipment::getRevenueDate, swStInternetTicketEquipmentDetails.getRevenueDate()));
            //删除：发售数量、发售金额置0，重新计算差异金额、差异数量
            swAuInternetTicketEquipment.setTotalNumberSales(0);
            swAuInternetTicketEquipment.setSubtotalTotal(new BigDecimal("0.00"));
            swAuInternetTicketEquipmentMapper.updateById(swAuInternetTicketEquipment);
        }
        return R.ok(swStInternetTicketEquipmentDetailsService.removeById(id));
    }

}
